home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / ZIP_1_0.lha / src / control.c < prev    next >
C/C++ Source or Header  |  1992-10-13  |  3KB  |  209 lines

  1. /*
  2.  * control.c
  3.  *
  4.  * Functions that alter the flow of control.
  5.  *
  6.  * Mark Howell 28-Jul-1992 V1.0
  7.  *
  8.  */
  9.  
  10. #include "ztypes.h"
  11.  
  12. /*
  13.  * call
  14.  *
  15.  * Call a subroutine. Save PC and FP then load new PC and initialise stack based
  16.  * local arguments.
  17.  *
  18.  */
  19.  
  20. #ifdef __STDC__
  21. void call (int argc, zword_t *argv)
  22. #else
  23. void call (argc, argv)
  24. int argc;
  25. zword_t *argv;
  26. #endif
  27. {
  28.     zword_t arg;
  29.     int i = 1, args;
  30.  
  31.     /* Convert calls to 0 as returning FALSE */
  32.  
  33.     if (argv[0] == 0) {
  34.         store_operand (FALSE);
  35.         return;
  36.     }
  37.  
  38.     /* Save current PC and FP on stack */
  39.  
  40.     stack[--sp] = (zword_t) (pc / PAGE_SIZE);
  41.     stack[--sp] = (zword_t) (pc % PAGE_SIZE);
  42.     stack[--sp] = fp;
  43.  
  44.     /* Create FP for new subroutine and load new PC */
  45.  
  46.     fp = sp - 1;
  47.     pc = (unsigned long) argv[0] * story_scaler;
  48.  
  49.     /* Read argument count and initialise local variables */
  50.  
  51.     args = (unsigned int) read_code_byte ();
  52.     while (--args >= 0) {
  53.         arg = read_code_word ();
  54.         stack[--sp] = (--argc > 0) ? argv[i++] : arg;
  55.     }
  56.  
  57. }/* call */
  58.  
  59. /*
  60.  * ret
  61.  *
  62.  * Return from subroutine. Restore FP and PC from stack.
  63.  *
  64.  */
  65.  
  66. #ifdef __STDC__
  67. void ret (zword_t value)
  68. #else
  69. void ret (value)
  70. zword_t value;
  71. #endif
  72. {
  73.  
  74.     /* Clean stack */
  75.  
  76.     sp = fp + 1;
  77.  
  78.     /* Restore FP and PC */
  79.  
  80.     fp = stack[sp++];
  81.     pc = stack[sp++];
  82.     pc += (unsigned long) stack[sp++] * PAGE_SIZE;
  83.  
  84.     /* Return subroutine value */
  85.  
  86.     store_operand (value);
  87.  
  88. }/* ret */
  89.  
  90. /*
  91.  * jump
  92.  *
  93.  * Unconditional jump. Jump is PC relative.
  94.  *
  95.  */
  96.  
  97. #ifdef __STDC__
  98. void jump (zword_t offset)
  99. #else
  100. void jump (offset)
  101. zword_t offset;
  102. #endif
  103. {
  104.  
  105.     pc = (unsigned long) (pc + (short) offset - 2);
  106.  
  107. }/* jump */
  108.  
  109. /*
  110.  * pop_ret
  111.  *
  112.  * Pop value from stack and return from subroutine with value.
  113.  *
  114.  */
  115.  
  116. #ifdef __STDC__
  117. void pop_ret (void)
  118. #else
  119. void pop_ret ()
  120. #endif
  121. {
  122.  
  123.     ret (stack[sp++]);
  124.  
  125. }
  126.  
  127. /*
  128.  * pop
  129.  *
  130.  * Pop stack and ignore value
  131.  *
  132.  */
  133.  
  134. #ifdef __STDC__
  135. void pop (void)
  136. #else
  137. void pop ()
  138. #endif
  139. {
  140.  
  141.     sp++;
  142.  
  143. }/* pop */
  144.  
  145. /*
  146.  * restart
  147.  *
  148.  * Restart game by initialising environment and reloading start PC.
  149.  *
  150.  */
  151.  
  152. #ifdef __STDC__
  153. void restart (void)
  154. #else
  155. void restart ()
  156. #endif
  157. {
  158.     unsigned int restart_size, i, scripting;
  159.  
  160.     /* Write any text in output buffer, restart the screen and randomise */
  161.  
  162.     flush_buffer (TRUE);
  163.     restart_screen ();
  164.     srand ((unsigned int) time (NULL));
  165.  
  166.     /* Initialise global state variables */
  167.  
  168.     lines_written = 0;
  169.     status_active = OFF;
  170.     status_size = 0;
  171.     window = TEXT_WINDOW;
  172.  
  173.     /* Load state of scripting flag */
  174.  
  175.     scripting = get_word (H_FLAGS) & SCRIPTING_FLAG;
  176.  
  177.     /* Load restart size and reload writeable data area */
  178.  
  179.     restart_size = (h_restart_size / PAGE_SIZE) + 1;
  180.     for (i = 0; i < restart_size; i++)
  181.         read_page (i, &datap[i * PAGE_SIZE]);
  182.  
  183.     /* Reset game header varibles */
  184.  
  185.     if (h_type == V3)
  186.         set_byte (H_CONFIG, (get_byte (H_CONFIG) | 0x20));
  187.     else
  188.         set_byte (H_CONFIG, 0x3f); /* Turn on all flags */
  189.     set_word (H_FLAGS, scripting);
  190.     set_byte (H_INTERPRETER, 12);
  191.     set_byte (H_INTERPRETER_VERSION, 'A');
  192.     set_byte (H_SCREEN_ROWS, screen_rows);
  193.     set_byte (H_SCREEN_COLUMNS, screen_cols);
  194.  
  195.     /* Initialise status region */
  196.  
  197.     if (h_type == V3) {
  198.         set_status_size (1);
  199.         blank_status_line ();
  200.     }
  201.  
  202.     /* Load start PC, SP and FP */
  203.  
  204.     pc = h_start_pc;
  205.     sp = STACK_SIZE;
  206.     fp = STACK_SIZE - 1;
  207.  
  208. }/* restart */
  209.